package com.turman.composebook.views

import android.annotation.SuppressLint
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Phone
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.turman.composebook.ui.theme.ComposeBookTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun MainPage() {
    ComposeBookTheme {
        var selectTab by rememberSaveable(Unit) {
            mutableStateOf(0)
        }
        val scope = rememberCoroutineScope()
        val scaffoldState = rememberDrawerState(initialValue = DrawerValue.Closed)
        val context = LocalContext.current
        Scaffold(
            modifier = Modifier.background(MaterialTheme.colorScheme.primary),
            topBar = {
                TopAppBar(title = { Text(text = "主页") }, navigationIcon = {
                    Icon(
                        imageVector = Icons.Default.Menu,
                        contentDescription = "menu",
                        modifier = Modifier
                            .clickable {
                                scope.launch {
                                    scaffoldState.open()
                                }
                            }
                            .padding(10.dp))
                }, actions = {
                    IconButton(onClick = {
                        Toast.makeText(context, "more", Toast.LENGTH_SHORT).show()
                    }) {
                        Icon(imageVector = Icons.Filled.MoreVert, contentDescription = "more")
                    }
                })
            },
            bottomBar = {
                NavigationBar(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(50.dp),
                    contentColor = MaterialTheme.colorScheme.primary
                ) {
                    Icon(
                        imageVector = Icons.Default.Phone,
                        contentDescription = "phone",
                        modifier = Modifier
                            .fillMaxHeight()
                            .width(0.dp)
                            .weight(1f)
                            .clickable {
                                selectTab = 0
                            })
                    Icon(imageVector = Icons.Default.Person, contentDescription = "person",
                        modifier = Modifier
                            .fillMaxHeight()
                            .width(0.dp)
                            .weight(1f)
                            .clickable {
                                selectTab = 1
                            })
                }
            }) {
            DrawerView(
                selectTab = selectTab,
                drawerState = scaffoldState,
                scope = scope,
                paddingValues = it,
                onMenuClick = {
                    selectTab = it
                    scope.launch {
                        scaffoldState.close()
                    }
                }
            )
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DrawerView(
    selectTab: Int,
    drawerState: DrawerState,
    scope: CoroutineScope,
    paddingValues: PaddingValues,
    onMenuClick: (Int) -> Unit
) {
    var currentMenuIndex by rememberSaveable(Unit) {
        mutableStateOf(-1)
    }
    ModalNavigationDrawer(drawerState = drawerState, drawerContent = {
        LazyColumn(
            verticalArrangement = Arrangement.Top,
            modifier = Modifier
                .fillMaxHeight()
                .width(280.dp)
                .padding(top = 65.dp, bottom = 50.dp)
                .background(MaterialTheme.colorScheme.background)
        ) {
            item {
                NavigationDrawerItem(
                    label = { Text(text = "Lottie Sample") },
                    selected = currentMenuIndex == 0,
                    onClick = {
                        onMenuClick(0)
                        currentMenuIndex = 0
                    })
                NavigationDrawerItem(
                    label = { Text(text = "Navigation Sample") },
                    selected = currentMenuIndex == 1,
                    onClick = {
                        onMenuClick(1)
                        currentMenuIndex = 1
                    })
                NavigationDrawerItem(
                    label = { Text(text = "ColumnRowBox Sample") },
                    selected = currentMenuIndex == 2,
                    onClick = {
                        onMenuClick(2)
                        currentMenuIndex = 2
                    })
                NavigationDrawerItem(
                    label = { Text(text = "ClicksAndSurface Sample") },
                    selected = currentMenuIndex == 3,
                    onClick = {
                        onMenuClick(3)
                        currentMenuIndex = 3
                    })
                NavigationDrawerItem(
                    label = { Text(text = "Text Sample") },
                    selected = currentMenuIndex == 4,
                    onClick = {
                        onMenuClick(4)
                        currentMenuIndex = 4
                    })
                NavigationDrawerItem(
                    label = { Text(text = "Button Sample") },
                    selected = currentMenuIndex == 5,
                    onClick = {
                        onMenuClick(5)
                        currentMenuIndex = 5
                    })
                NavigationDrawerItem(
                    label = { Text(text = "TextField Sample") },
                    selected = currentMenuIndex == 6,
                    onClick = {
                        onMenuClick(6)
                        currentMenuIndex = 6
                    })
                NavigationDrawerItem(
                    label = { Text(text = "Image Sample") },
                    selected = currentMenuIndex == 7,
                    onClick = {
                        onMenuClick(7)
                        currentMenuIndex = 7
                    })
                NavigationDrawerItem(
                    label = { Text(text = "LazyColumn Sample") },
                    selected = currentMenuIndex == 8,
                    onClick = {
                        onMenuClick(8)
                        currentMenuIndex = 8
                    })
                NavigationDrawerItem(
                    label = { Text(text = "LazColumn Sample 1") },
                    selected = currentMenuIndex == 9,
                    onClick = {
                        onMenuClick(9)
                        currentMenuIndex = 9
                    })
                NavigationDrawerItem(
                    label = { Text(text = "LazyRow Sample") },
                    selected = currentMenuIndex == 10,
                    onClick = {
                        onMenuClick(10)
                        currentMenuIndex = 10
                    })
                NavigationDrawerItem(
                    label = { Text(text = "StickyHeader Sample") },
                    selected = currentMenuIndex == 11,
                    onClick = {
                        onMenuClick(11)
                        currentMenuIndex = 11
                    })
                NavigationDrawerItem(
                    label = { Text(text = "LazyVerticalGrid Sample") },
                    selected = currentMenuIndex == 12,
                    onClick = {
                        onMenuClick(12)
                        currentMenuIndex = 12
                    })
                NavigationDrawerItem(
                    label = { Text(text = "LazyVerticalGrid Sample 1") },
                    selected = currentMenuIndex == 13,
                    onClick = {
                        onMenuClick(13)
                        currentMenuIndex = 13
                    })
                NavigationDrawerItem(
                    label = { Text(text = "ListItem Sample") },
                    selected = currentMenuIndex == 14,
                    onClick = {
                        onMenuClick(14)
                        currentMenuIndex = 14
                    })
                NavigationDrawerItem(
                    label = { Text(text = "LazyListLayoutInfo Sample") },
                    selected = currentMenuIndex == 15,
                    onClick = {
                        onMenuClick(15)
                        currentMenuIndex = 15
                    })
                NavigationDrawerItem(
                    label = { Text(text = "TopAppBar Sample") },
                    selected = currentMenuIndex == 16,
                    onClick = {
                        onMenuClick(16)
                        currentMenuIndex = 16
                    })
                NavigationDrawerItem(
                    label = { Text(text = "BottomNavigation Sample") },
                    selected = currentMenuIndex == 17,
                    onClick = {
                        onMenuClick(17)
                        currentMenuIndex = 17
                    })
                NavigationDrawerItem(
                    label = { Text(text = "ModalDrawer Sample") },
                    selected = currentMenuIndex == 18,
                    onClick = {
                        onMenuClick(18)
                        currentMenuIndex = 18
                    })
                NavigationDrawerItem(
                    label = { Text(text = "BottomSheet Sample") },
                    selected = currentMenuIndex == 19,
                    onClick = {
                        onMenuClick(19)
                        currentMenuIndex = 19
                    })
                NavigationDrawerItem(
                    label = { Text(text = "ModelBottomSheetLayout Sample") },
                    selected = currentMenuIndex == 20,
                    onClick = {
                        onMenuClick(20)
                        currentMenuIndex = 20
                    })
                NavigationDrawerItem(
                    label = { Text(text = "BottomDrawer Sample") },
                    selected = currentMenuIndex == 21,
                    onClick = {
                        onMenuClick(21)
                        currentMenuIndex = 21
                    })
                NavigationDrawerItem(
                    label = { Text(text = "BottomDrawer Sample 2") },
                    selected = currentMenuIndex == 22,
                    onClick = {
                        onMenuClick(22)
                        currentMenuIndex = 22
                    })
                NavigationDrawerItem(
                    label = { Text(text = "BackDropScaffold Sample") },
                    selected = currentMenuIndex == 23,
                    onClick = {
                        onMenuClick(23)
                        currentMenuIndex = 23
                    })
                NavigationDrawerItem(
                    label = { Text(text = "SnackProgressSelection Sample") },
                    selected = currentMenuIndex == 24,
                    onClick = {
                        onMenuClick(24)
                        currentMenuIndex = 24
                    })
                NavigationDrawerItem(
                    label = { Text(text = "AlertDialog Sample") },
                    selected = currentMenuIndex == 25,
                    onClick = {
                        onMenuClick(25)
                        currentMenuIndex = 25
                    })


                NavigationDrawerItem(
                    label = { Text(text = "Close") },
                    selected = false,
                    onClick = {
                        scope.launch {
                            drawerState.close()
                        }
                        currentMenuIndex = -1
                    })
            }
        }
    }) {
        val modifier = Modifier.padding(paddingValues)
        when (selectTab) {
            0 -> LottieSampleView(modifier)
            1 -> NavigationSampleView(modifier)
            2 -> ColumnRowBoxSampleView(modifier)
            3 -> ClicksAndSurfaceView(modifier)
            4 -> TextSampleView(modifier)
            5 -> ButtonSampleView(modifier)
            6 -> TextFieldSampleView(modifier)
            7 -> ImageSampleView(modifier)
            8 -> LazyColumnSampleView(modifier)
            9 -> LazyColumnSampleView1(modifier)
            10 -> LazyRowSampleView(modifier)
            11 -> StickyHeaderSampleView(modifier)
            12 -> LazyVerticalGridSampleView(modifier)
            13 -> LazyVerticalGridSampleView1(modifier)
            14 -> ListItemSampleView(modifier)
            15 -> LazyListLayoutInfoSampleView(modifier)
            16 -> TopAppBarSampleView(modifier)
            17 -> BottomNavigationSampleView(modifier)
            18 -> ModalDrawerSampleView(modifier)
            19 -> BottomSheetSampleView(modifier)
            20 -> ModelBottomSheetLayoutSampleView(modifier)
            21 -> BottomDrawerSampleView(modifier)
            22 -> BottomDrawerSampleView2(modifier)
            23 -> BackDropScaffoldSampleView(modifier)
            24 -> SnackProgressSelectionSampleView(modifier)
            25 -> AlertDialogSampleView(modifier)
        }
    }
}





















